﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;

namespace LimpaGaragem.DAO.Product
{
    public class Rate
    {
        /// <summary>
        /// Return list of rate from product
        /// </summary>
        /// <returns></returns>
        public List<LimpaGaragem.Model.Product.Rate> GetListOfRates(int IDProduct)
        {
            try
            {
                Framework.DataAccess objDataAccess = new Framework.DataAccess();

                //PARAMETERS
                List<SqlParameter> objListOfParameters = new List<SqlParameter>();
                objListOfParameters.Add(new SqlParameter("@ID_PRODUCT", IDProduct));
                DataTable objDataTable = objDataAccess.ReturnDataTable(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString, Framework.DataAccess.QueryType.Procedure, "PR014_ListProductRates", 0, objListOfParameters);

                List<LimpaGaragem.Model.Product.Rate> objListOfRate = new List<LimpaGaragem.Model.Product.Rate>();
                foreach (DataRow objDataRow in objDataTable.Rows)
                {
                    LimpaGaragem.Model.Product.Rate objRate = new LimpaGaragem.Model.Product.Rate();
                    objRate.IDProduct = IDProduct;
                    objRate.ID = Convert.ToInt32(objDataRow["ID_RATE"]);
                    objRate.IDUser = Convert.ToInt32(objDataRow["ID_USER"]);
                    objRate.IDScoreRate = Convert.ToInt32(objDataRow["ID_RATE_SCORE"]);
                    objRate.Content = objDataRow["DS_CONTENT"].ToString();
                    objRate.DateTimeInserted = Convert.ToDateTime(objDataRow["DT_INC"]);
                    objListOfRate.Add(objRate);
                }
                return objListOfRate;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Save rate
        /// </summary>
        public void SaveRate(int IDProduct,
                            int IDUser,
                            int IDScoreRate,
                            string Content)
        {
            try
            {
                Framework.DataAccess objDataAccess = new Framework.DataAccess();

                //PARAMETERS
                List<SqlParameter> objListOfParameters = new List<SqlParameter>();
                objListOfParameters.Add(new SqlParameter("@ID_PRODUCT", IDProduct));
                objListOfParameters.Add(new SqlParameter("@ID_USER", IDUser));
                objListOfParameters.Add(new SqlParameter("@ID_RATE_SCORE", IDScoreRate));
                objListOfParameters.Add(new SqlParameter("@DS_CONTENT", Content));

                //EXECUTE
                objDataAccess.ExecuteQuery(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString, Framework.DataAccess.QueryType.Procedure, "PR014_SaveProductRate", 0, objListOfParameters);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
